<template>
    <view class="content">

        <u-cell-group>
            <!-- <u-cell title="清除缓存" value="12.56MB"></u-cell> -->
            <u-cell title="修改个人信息" isLink @click="toEditUserInfo"></u-cell>
            <u-cell title="实名认证" isLink @click="toAuthentication"></u-cell>
            <u-cell title="换绑手机号" isLink @click="toEditPhone"></u-cell>
            <u-cell title="隐私政策" isLink url="/pages/setting/policy"></u-cell>
            <u-cell title="退出登录" isLink @click="exitDialog"></u-cell>
        </u-cell-group>
        <u-modal :show="showTip" content='确定退出登录?' :showCancelButton="true" @confirm="exit"
            @cancel="() => { showTip = false }"></u-modal>
        <u-toast ref="toast"></u-toast>
    </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onShow } from "@dcloudio/uni-app";
import { userIsCertified } from '@/api/mine/mine'

const isCertified = ref(false)
const isLogin = ref(false)
const showTip = ref(false)
const toast: any = ref(null)

const toEditUserInfo = () => {
    if (isLogin.value) {
        uni.navigateTo({
            url: '/pages/setting/editUserInfo',
        })
    } else {
        toast.value.show({
            type: 'warning',
            message: "暂无用户登录",
            position: 'top',
            duration: 3000
        })
    }
}

const toAuthentication = () => {
    if (isLogin.value) {
        if (isCertified.value) {
            uni.navigateTo({
                url: '/pages/setting/certified',
            })
        } else {
            uni.navigateTo({
                url: '/pages/setting/authentication',
            })
        }
    } else {
        toast.value.show({
            type: 'warning',
            message: "暂无用户登录",
            position: 'top',
            duration: 3000
        })
    }

}

const toEditPhone = () => {
    if (isLogin.value) {
        if (isCertified.value) {
            uni.navigateTo({
                url: '/pages/setting/editPhone',
            })
        } else {
            toast.value.show({
                type: 'warning',
                message: "请先完成实名认证",
                position: 'top',
                duration: 3000
            })
        }
    } else {
        toast.value.show({
            type: 'warning',
            message: "暂无用户登录",
            position: 'top',
            duration: 3000
        })
    }
}

const exitDialog = () => {
    if (isLogin.value) {
        showTip.value = true
    } else {
        toast.value.show({
            type: 'warning',
            message: "暂无用户登录",
            position: 'top',
            duration: 3000
        })
    }
}

const exit = () => {
    showTip.value = false
    uni.removeStorage({
        key: 'user_token',
        success: function () {
            toast.value.show({
                type: 'success',
                message: "退出登录成功",
                position: 'top',
                duration: 3000,
                complete: function () {
                    uni.switchTab({
                        url: '/pages/mine/mine'
                    });
                }
            })

        },
        fail: function () {
            toast.value.show({
                type: 'warning',
                message: "退出登录失败",
                position: 'top',
                duration: 3000
            })
        }
    });
}

onShow(() => {
    uni.getStorage({
        key: 'user_token',
        success: (res) => {
            isLogin.value = true
            userIsCertified().then((res: any) => {
                if (res.code == 200) {
                    if (res.data.is_certified == '已认证') {
                        isCertified.value = true
                    }
                }
            })
        },
        fail: (err) => {
            isLogin.value = false
        }
    })
})

</script>

<style lang="scss" scoped>
.content {
    border-top: 2rpx solid #E4E4E4;
}
</style>